<template>
<div class="addSubject">
    <el-button type="primary" size="mini" style="margin-right: 10px" @click="relevancySubjectShow = true">
        <slot>关联主体</slot>
    </el-button>

    <el-dialog title="新增生产主体" :append-to-body="true" :visible.sync="AddDialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false">
        <el-card>
            <el-tabs v-model="typeClass" stretch>
                <el-tab-pane label="个体户" name="个体户">
                    <Individual @on-close="closeAddModel()" @success="addIndividual"></Individual>
                </el-tab-pane>
                <el-tab-pane label="合作社" name="合作社">
                    <Cooperation @on-close="closeAddModel()" @success="addIndividual"></Cooperation>
                </el-tab-pane>
                <el-tab-pane label="企业" name="企业">
                    <Enterprise @on-close="closeAddModel()" @success="addIndividual"></Enterprise>
                </el-tab-pane>
            </el-tabs>
        </el-card>
        <!--		<el-button @click="AddDialogFormVisible = false" type="default" class="btn-block" style="margin-top: 15px">-->
        <!--		关闭窗口</el-button>-->
    </el-dialog>

    <el-dialog title="查询主体" :close-on-click-modal="true" :visible.sync="searchSubjectShow" append-to-body width="80%">
        <el-card style="margin-bottom: 20px;">
            <el-row :gutter="20">
                <el-col :span="8">

                    <el-input type="text" placeholder="请输入主体名称（模糊搜索）" v-model="keywords"></el-input>

                </el-col>
                <el-col :span="6">
                    <el-button type="primary" @click.native="filterSubjectForm">查询</el-button>
                    <el-button type="info" @click.native="AddDialogFormVisible = true">新增生产主体</el-button>
                </el-col>
            </el-row>

        </el-card>

        <el-card v-if="SubjectForm.length">
            <el-row :gutter="20">
                <el-col :span="8" v-for="(item, index) in SubjectForm" :key="index">
                    <el-card shadow="never" class="result" @click.native="insertion(item)" :key="index">
                        <h4 class="title">
                            <span><span>主体名称:</span>{{ item.subjectName }} </span>
                             <div> <el-button type="success" size="mini" @click.native="filterSubjectForm">选择</el-button>
                            <el-button type="danger" size="mini" @click.stop="handleClick(item,'edit')">编辑</el-button></div>
                        </h4>
                        <p v-if="item.subjectTypeName === '个体户'">
                            <span>公民身份号码:</span>{{ item.subjectCard }}
                        </p>
                        <p v-else><span>主体组织机构代码:</span>{{ item.subjectCard }}</p>
                        <p><span>主体类别:</span>{{ item.subjectTypeName }}</p>
                        <p><span>手机号码:</span>{{ item.subjectTele }}</p>
                        <p><span>主体地址:</span>{{ item.subjectAddr }}</p>
                        <p><span>主体基地:</span>{{ item.subjectBase }}</p>
                        <p :title="item.subjectIntro"><span>主体介绍:</span>{{ item.subjectIntro.length>100?item.subjectIntro.substring(0,100)+'...':item.subjectIntro }}</p>
                    </el-card>

                </el-col>
            </el-row>

            <div class="dialog-footer" style="float: right; padding: 15px 0;display: none">
                <el-button type="info" @click="searchSubjectShow = false">关闭查询窗口</el-button>
            </div>
        </el-card>
    </el-dialog>

    <el-dialog title="关联主体" :append-to-body="true" :visible.sync="relevancySubjectShow">
        <el-form :model="form" autocomplete="off">
            <el-form-item label="经营主体">
                <el-input @click.native="searchSubject" v-model="form.subjectName" :readonly="true"></el-input>
            </el-form-item>
            <el-form-item label="占用面积(亩)">
                <el-input v-model="form.lsSubjectUseArea" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="年产值(万元)">
                <el-input v-model="form.lsYearValue" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="承包周期">
                <el-date-picker v-model="form.date" type="daterange" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="承包年限">
                <el-input v-model="form.contractYear" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="所属基地">
                <el-input v-model="form.subjectBase" autocomplete="off"></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="relevancySubjectShow = false">取 消</el-button>
            <el-button type="primary" @click="submit()">确 定</el-button>
        </div>
    </el-dialog>


    <el-dialog title="主体详情" :append-to-body="true" :visible.sync="DetailsDialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false">
        <el-card>
            <Look @success="editIndividual" @on-close="DetailsDialogFormVisible = false" :editor="editor" :subjectFormRow='subjectFormRow'></Look>
        </el-card>
        <!--            <el-button @click="DetailsDialogFormVisible = false" type="danger" class="btn-block"-->
        <!--                       style="margin-top: 15px">关闭窗口</el-button>-->
    </el-dialog>
</div>
</template>

<script>
import Individual from "@/views/DataResource/ProductionBase/Individual/Individual.vue";
import Cooperation from "@/views/DataResource/ProductionBase/Cooperation/Cooperation";
import Enterprise from "@/views/DataResource/ProductionBase/Enterprise/Enterprise";
import Look from "@/views/DataResource/ProductionBase/Look/Look";
import {
    formatDate
} from "@/library";
import {
    mapState
} from "vuex";
export default {
    name: "",
    props: {
        landCodeT: {
            type: [String, Array],
            required: true,
        },
        addType: {
            type: Number,
            default: 0,
        },
        // tableData: {
        // 	type: Array
        // }
    },
    components: {
        Individual,
        Cooperation,
        Enterprise,
        Look
    },
    data() {
        return {
            AddDialogFormVisible: false,
            pickerOptions: {
                shortcuts: [{
                        text: "最近一年",
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90 * 4);
                            picker.$emit("pick", [start, end]);
                        },
                    },
                    {
                        text: "最近三年",
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90 * 12);
                            picker.$emit("pick", [start, end]);
                        },
                    },
                ],
            },
            typeClass: "个体户",
            keywords: "",
            formLabelWidth: "100px",
            relevancySubjectShow: false,
            searchSubjectShow: false,
            emitForm: {},
            form: {
                date: "",
                contractYear: "",
                subjectName: "",
                lsSubjectUseArea: "",
                lsYearValue: "",
                base: "默认基地",
            },
            params: {
                pageNumber: 1,
                pageSize: 10,
            },
            paging: {},
            SearchFrom: [],
            SubjectForm: [],
            tableData: [],
            List: [],
            SubjectInfoList: [],
            multipleSelection: [],
            AddDialogFormVisible: false,
            DetailsDialogFormVisible:false,
            editor: true,
            subjectFormRow: {},
        };
    },
    computed: {
                ...mapState({
            taskObject: (state) => {
                return state.taskmodel.taskObject;
            },
            currentTask: (state) => {
                return state.currentTask.Task;
            },
            userInfo: (state) => {
                return state.User.userInfo;
            },
        })
    },
    methods: {
        handleClick(row, type) {
            console.log(row);
            this.subjectFormRow = row;
            this.editor = type === 'look' ? true : false;
            this.DetailsDialogFormVisible = true
        },
        editIndividual(res) {
            this.DetailsDialogFormVisible = res.visible;
            if (res.code === 200) {
                this.getSubjectInfoList();
                this.filterSubjectForm()
            }
        },
        addIndividual(res) {
            this.AddDialogFormVisible = res.visible;
            if (res.code === 200) {
                this.getSubjectInfoList();
            }
        },
        openAdd() {
            this.AddDialogFormVisible = true;
        },
        closeAddModel() {
            this.AddDialogFormVisible = false;
        },
        filterSubjectForm() {
            this.SubjectForm = [];
            var keyword = this.keywords;
            if (keyword) {
                this.$axios
                    .get("/rbac/data/subject/getSubjectInfoList", {
                        params: {
                            "subjectInfo.subjectName": keyword.trim(),
                        },
                    })
                    .then((res) => {
                        if (res.data.list instanceof Array && res.data.list.length) {
                            this.SubjectForm = res.data.list;
                        } else {
                            this.$message({
                                type: "warning",
                                message: "没有查询到对应的主体信息",
                            });
                            this.SubjectForm = [];
                        }
                    });
            } else {
                this.$message({
                    type: "warning",
                    message: "主体名称关键字不能为空",
                });
            }
        },
        insertion(item) {
            this.form.subjectName = item.subjectName;
            this.from = item;
            this.emitForm = item;
            this.searchSubjectShow = false;
        },
        searchSubject() {
            this.searchSubjectShow = true;
        },

        submit() {
            let landCodeT
            if(this.landCodeT instanceof Array){
                landCodeT =  this.landCodeT.join(',')
            }else{
                landCodeT = this.landCodeT
            }
            this.$axios
                .post("rbac/data/LandSubject/add", {
                    params: {
                        // lsLandCode: landCodeT,
                        landIds:landCodeT,
                        lsSubjectCode: this.emitForm.subjectCode,
                        landSubjectBase: this.form.subjectBase,
                        contractStartTime: formatDate(+new Date(this.form.date[0])),
                        lsSubjectUseArea: this.form.lsSubjectUseArea,
                        lsYearValue: this.form.lsYearValue,
                        contractEndTime: formatDate(+new Date(this.form.date[1])),
                        contractYear: this.form.contractYear,
                        planId:this.currentTask.data.id
                    },
                })
                .then((res) => {
                    if (res.errorCode == "00000") {
                        this.$axios
                            .get("rbac/data/LandSubject/get", {
                                params: {
                                    "landSubject.lsLandCode": landCodeT,
                                },
                            })
                            .then((res) => {
                                if (res.errorCode == "00000") {
                                    this.$emit("on-success", res.data.list);
                                }
                            });
                    } else {
                        this.$message({
                            type: "warning",
                            message: res.errorMsg,
                        });
                    }
                    this.relevancySubjectShow = false;
                });
        },
    },
};
</script>

<style lang="scss" scoped>
.result {
    margin: 10px 0;
    border: 1px solid #ddd;
    border-radius: 5px;

    h4 {
        padding-bottom: 15px;
        border-bottom: 1px solid #ddd;
        margin-bottom: 15px;
    }

    .title {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    p {
        line-height: 22px;
    }
}
</style>
